import { getAllDicts } from '@/http/moudules/sys/dict'
import { findAll } from '@/http/moudules/Basic/Matertial/item'
const state = {
  dicts: {},
  item: [],
  initDict: false,
  initItem: false
}

const mutations = {
  SET_DICT: (state, data) => {
 
    if (state.initDict) {
      return;
    }
    state.initDict = true
    for (let i = 0, len = data.length; i < len; i++) {
  
      let item = data[i];
      if(item.type==1){
        var dtype=item.dType;
        if (state.dicts[dtype] != undefined) {
          state.dicts[dtype].push(item);
         
        } else {
          state.dicts[dtype] = [];
          state.dicts[dtype].push(item);
         
        }


      }
      
    }
  },
  SET_ITEM: (state, res) => {
    state.initItem = true
    state.item = res;
  }


}

const actions = {
  // user login
  getAllDicts({ commit }) {
    if (this.state.dict.initDict) {
      return new Promise((resolve, reject) => { resolve() })
    } else {
      return new Promise((resolve, reject) => {
        getAllDicts().then((res) => {
          commit('SET_DICT', res.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })

    }

  },
  getAllItem({ commit }) {
    if (this.state.dict.initItem) {
      return new Promise((resolve, reject) => { resolve() })
    }
    return new Promise((resolve, reject) => {
      findAll().then((res) => {
        commit('SET_ITEM', res.data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })



  }
}

export default {

  state,
  mutations,
  actions
}
